<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="year" size="mini" @change="changeDate">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="month" size="mini" @change="changeDate">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <!-- 解决日历不实时更新语言切换问题 -->
    <el-calendar v-if="isShow" v-model="curDate">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeekend(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
const curDate = new Date() // 当前日期时间
const year = curDate.getFullYear()
const month = curDate.getMonth() + 1
const yearList = []

// 前后5年的年份集合
for (let i = 0; i < 11; i++) {
  const newYear = year - 5 + i
  yearList.push(newYear)
}
export default {
  filters: {
    getDay(val) {
      return Number(val.split('-')[2])
    }
  },
  data() {
    return {
      curDate: curDate,
      year: year, // 当前年份
      month: month, // 当前月份
      yearList: yearList, // 前后5年的年份集合
      isShow: true
    }
  },
  watch: {
    // 侦听 i18n 的方法的变化, 侦听器中不需要 this. 获取
    // 解决日历组件 语言不实时更新问题
    '$i18n.locale'() {
      console.log(this.$i18n.locale)
      this.isShow = false
      this.$nextTick(() => {
        this.isShow = true
      })
    },
    curDate() {
    //   console.log(this.curDate)
      this.year = this.curDate.getFullYear()
      this.month = this.curDate.getMonth() + 1
    }
  },
  methods: {
    changeDate() {
      this.curDate = new Date(`${this.year}-${this.month}`)
    },
    isWeekend(date) {
      return date.getDay() === 6 || date.getDay() === 0 // 判断当前时间是否为周末
    }
  }
}
</script>

<style  scoped>
 .el-calendar-day {
  height: auto;
}
 .el-calendar-table__row td,
 .el-calendar-table tr td:first-child,
 .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
 .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
 .el-calendar__header {
  display: none;
}
</style>
